/*
 * Lenovo Group
 * Copyright (c) 2015-2016 All Rights Reserved.
 * Project Name: lmrp-android app
 * Create Time: 16-2-16 下午6:47
 */

package com.lenovo.mso2o.util;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.hardware.Camera.Size;
import android.widget.Toast;

import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.utils.route.BaiduMapRoutePlan;
import com.baidu.mapapi.utils.route.RouteParaOption;
import com.lenovo.framework.base.InjectableFragmentActivity;
import com.lenovo.framework.biz.ContextManager;
import com.lenovo.framework.util.DLog;
import com.lenovo.mso2o.R;
import com.lenovo.mso2o.ui.fragment.GuideFragment;

import java.util.Comparator;
import java.util.List;

import rx.Observable;
import rx.Subscriber;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;

public class Utils {
    public static Subscriber<? super String> subscriber;
    private static long prev = 0;
    private static Subscription _subscription;
	private static Toast toast ;
	private Utils() {
	};

	/*public static String getTypeInChinese(Context contextChild, String english) {
		if (contextChild.getString(R.string.graborder_eng).equals(english)) {
			return contextChild.getString(R.string.graborder_chinese);
		} else if (contextChild.getString(R.string.receiveorder_eng).equals(english)) {
			return contextChild.getString(R.string.receiveorder);
		} else if (contextChild.getString(R.string.appoint_eng).equals(english)) {
			return contextChild.getString(R.string.appoint);
		} else if (contextChild.getString(R.string.arrived_eng).equals(english)) {
			return contextChild.getString(R.string.reached);
		} else if (contextChild.getString(R.string.finish_eng).equals(english)) {
			return contextChild.getString(R.string.finish);
		} else if (contextChild.getString(R.string.evaluate_eng).equals(english)) {
			return contextChild.getString(R.string.evaluate);
		}
		return "";
	}*/


	/**
	 * 选择本地导航或者第三方导航
	 */
	public static void showChannelDialog(final LatLng start, final LatLng end, final Context context) {
		new AlertDialog.Builder(context).setTitle("请选择导航方式")
				.setSingleChoiceItems(new String[]{"本地导航", "百度导航"}, 0, new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						switch (which) {
							case 0:
								Intent intent = new Intent(context, InjectableFragmentActivity.class);
								intent.putExtra(GuideFragment.END_LAT, end.latitude);
								intent.putExtra(GuideFragment.END_LOG, end.longitude);
								intent.putExtra(InjectableFragmentActivity.FRAGMENT_CLASS_NAME, GuideFragment.class.getName());
								context.startActivity(intent);
								break;
							case 1:
								BaiduMapRoutePlan.setSupportWebRoute(true);
								// 构建 route搜索参数以及策略，起终点也可以用name构造
								RouteParaOption para = new RouteParaOption()
										.startPoint(start)
										.endPoint(end)
										.busStrategyType(RouteParaOption.EBusStrategyType.bus_recommend_way);
								try {
									BaiduMapRoutePlan.openBaiduMapTransitRoute(para, context);
								} catch (Exception e) {
									e.printStackTrace();
								}
								//结束调启功能时调用finish方法以释放相关资源
								break;
						}
						dialog.dismiss();
					}
				}).show();
	}


	public static List<Size> getResolutionList(Camera camera)
	{
		Parameters parameters = camera.getParameters();
		List<Size> previewSizes = parameters.getSupportedPreviewSizes();
		return previewSizes;
	}

	//工单编号，登录账号，操作类型（接单/抢单/预约/到场/完成/评价），经度，维度，APP操作时间，APP版本
	public static void getUserSyncInfo(String s){

	}

	public static class ResolutionComparator implements Comparator<Size> {

		@Override
		public int compare(Size lhs, Size rhs) {
			if(lhs.height!=rhs.height)
			return lhs.height-rhs.height;
			else
			return lhs.width-rhs.width;
		}
	}
	
	public static void leeLog(String s){
		DLog.e("LEE", s);
	}


    public static void debounceToast(final String text){
        if(_subscription == null)
            _subscription = Observable.create(new Observable.OnSubscribe<String>() {

                @Override
                public void call(Subscriber<? super String> subscriber) {
                    Utils.subscriber = subscriber;
                }
            })
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Action1<String>() {
                @Override
                public void call(String s) {
                    if(toast == null) {
                        toast = Toast.makeText(ContextManager.context(), "", Toast.LENGTH_SHORT);
                    }
                    if(System.currentTimeMillis() - prev > 2000) {
                        toast.setText(s);
                        toast.show();
                    }
                    DLog.i("toast time delta:", "" + (System.currentTimeMillis() - prev));
                    prev = System.currentTimeMillis();
                }
            });
        subscriber.onNext(text);
    }

    public static void unsubscribe(){
        if(_subscription != null)
            _subscription.unsubscribe();
    }
}
